package src.com.neusoft.nep.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AQI {
    private List<Map<String, String>> dataList;
    private Map<Integer, Integer> aqiLevelCountMap = new HashMap<>(); // 统计AQI等级

    // 存储每月超标统计数据（键："年-月"，值：超标次数）
    private Map<String, Integer> monthlyExceedCounts = new HashMap<>();

    public int coNum = 0;
    public int so2Num = 0;
    public int pmNum = 0;
    public int aqiNum = 0;

    public int getCoNum() {
        return coNum;
    }

    public int getSo2Num() {
        return so2Num;
    }

    public int getPmNum() {
        return pmNum;
    }

    public int getAqiNum() {
        return aqiNum;
    }

    public void analyze() {
        try {
            dataList = AdFileUtils.readListMapObject("AdminData.txt");
            System.out.println("成功读取 " + dataList.size() + " 条数据");
        } catch (Exception e) {
            System.err.println("读取数据失败: " + e.getMessage());
            dataList = List.of();
        }

        // 重置统计数据
        coNum = 0;
        so2Num = 0;
        pmNum = 0;
        aqiNum = 0;
        aqiLevelCountMap.clear();
        monthlyExceedCounts.clear();

        for (Map<String, String> row : dataList) {
            // 读取污染物浓度
            double so2 = Double.parseDouble(row.getOrDefault("so2", "0"));
            double co = Double.parseDouble(row.getOrDefault("co", "0"));
            double pm = Double.parseDouble(row.getOrDefault("pm", "0"));

            // 计算污染物等级
            int so2Level = getLevel(so2, "so2");
            int coLevel = getLevel(co, "co");
            int pmLevel = getLevel(pm, "pm");

            // 计算AQI等级
            int aqiLevel = Math.max(Math.max(so2Level, coLevel), pmLevel);

            // 存储等级信息
            row.put("so2Level", String.valueOf(so2Level));
            row.put("coLevel", String.valueOf(coLevel));
            row.put("pmLevel", String.valueOf(pmLevel));
            row.put("aqiLevel", String.valueOf(aqiLevel));

            // 统计超标数量
            if (so2Level >= 3) so2Num++;
            if (coLevel >= 3) coNum++;
            if (pmLevel >= 3) pmNum++;
            if (aqiLevel >= 3) aqiNum++;

            // 统计AQI等级的出现次数
            aqiLevelCountMap.put(aqiLevel, aqiLevelCountMap.getOrDefault(aqiLevel, 0) + 1);

            // 统计每月超标次数
            String dateStr = row.get("date");
            if (dateStr != null) {
                try {
                    String[] dateParts = dateStr.split("-");
                    if (dateParts.length >= 2) {
                        String monthKey = dateParts[0] + "-" + dateParts[1];
                        if (aqiLevel >= 3) {
                            monthlyExceedCounts.put(monthKey, monthlyExceedCounts.getOrDefault(monthKey, 0) + 1);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("AQI等级统计结果: " + aqiLevelCountMap);
        System.out.println("超标统计: so2=" + so2Num + ", co=" + coNum + ", pm=" + pmNum + ", aqi=" + aqiNum);
        System.out.println("每月超标统计结果: " + monthlyExceedCounts);
    }

    public int getLevel(double value, String type) {
        switch (type) {
            case "so2":
                if (value <= 50) return 1;
                else if (value <= 150) return 2;
                else if (value <= 475) return 3;
                else if (value <= 800) return 4;
                else if (value <= 1600) return 5;
                else return 6;
            case "co":
                if (value <= 5) return 1;
                else if (value <= 10) return 2;
                else if (value <= 35) return 3;
                else if (value <= 60) return 4;
                else if (value <= 90) return 5;
                else if (value <= 150) return 6;
                else return 6;
            case "pm":
                if (value <= 35) return 1;
                else if (value <= 75) return 2;
                else if (value <= 115) return 3;
                else if (value <= 150) return 4;
                else if (value <= 250) return 5;
                else if (value <= 500) return 6;
                else return 6;
            case "aqi":
                if (value <= 50) return 1;
                else if (value <= 100) return 2;
                else if (value <= 150) return 3;
                else if (value <= 200) return 4;
                else if (value <= 300) return 5;
                else return 6;
            default:
                System.err.println("未知类型: " + type);
                return 0;
        }
    }

    // 返回AQI等级统计结果
    public Map<Integer, Integer> getAQIStatistics() {
        return new HashMap<>(aqiLevelCountMap);
    }

    // 保持原方法名，提供相同的统计结果
    public Map<Integer, Integer> getAQILevelStatistics() {
        return getAQIStatistics();
    }

    // 获取指定月份的超标累计数量
    public int getExceedCountByMonth(String monthKey) {
        return monthlyExceedCounts.getOrDefault(monthKey, 0);
    }

    // 获取所有月份的超标统计数据
    public Map<String, Integer> getMonthlyExceedCounts() {
        return monthlyExceedCounts;
    }
    public void reset() {
        aqiStatistics.clear();
    }

    // 更新AQI等级统计
    public void updateAQILevel(int level) {
        aqiStatistics.put(level, aqiStatistics.getOrDefault(level, 0) + 1);
    }
    private Map<Integer, Integer> aqiStatistics = new HashMap<>();
}